/******************************************************************************

Copyright (C), 2005-2014, Hisilicon Tech. Co., Ltd.
******************************************************************************
File Name     : ca_cfg.c
Version       : Initial
Author        : Hisilicon hisecurity group
Created       : 2013-08-28
Last Modified :
Description   :  
Function List :
History       :
******************************************************************************/
/*this file should be configured by customer*/
#include "hi_type.h"
#include "ca_verify_def.h"
#include "hi_unf_cipher.h"


/*nand and SPI max flash block size*/
HI_U32 g_MaxBlockSize =  0x00100000;//(1M)

#define KEY_LEN 16

/*Address of bootenv parameters in flash*/

#ifdef HI_MINIBOOT_SUPPORT
#ifdef CONFIG_ENV_ADDR     //it's defined in source/boot/Makefile.miniboot , value comes from  CFG_HI_BOOT_ENV_STARTADDR defined in cfg.mak
HI_U32 g_EnvFlashAddr = CONFIG_ENV_ADDR;
#else
HI_U32 g_EnvFlashAddr = 0x000A0000;      //640KB
#endif

#else

#ifdef CFG_ENV_ADDR     //it's defined in source/boot/Makefile.fastboot , value comes from  CFG_HI_BOOT_ENV_STARTADDR defined in cfg.mak
HI_U32 g_EnvFlashAddr = CFG_ENV_ADDR;    
#else
HI_U32 g_EnvFlashAddr = 0x000A0000;      //640KB
#endif

#endif

/*Size of bootenv parameters in flash*/

#ifdef HI_MINIBOOT_SUPPORT

#ifdef CONFIG_ENV_RANGE    //it's it's defined in source/boot/Makefile.miniboot, value comes from  CFG_HI_BOOT_ENV_STARTADDR defined in cfg.mak
HI_U32 g_EnvFlashPartionSize =  CONFIG_ENV_RANGE;
#else
HI_U32 g_EnvFlashPartionSize =  0x00020000; //128K
#endif

#else

#ifdef CFG_ENV_RANGE    //it's defined in source/boot/Makefile.fastboot , value comes from  CFG_HI_BOOT_ENV_RANGE defined in cfg.mak
HI_U32 g_EnvFlashPartionSize =  CFG_ENV_RANGE;   
#else
HI_U32 g_EnvFlashPartionSize =  0x00020000; //128K
#endif

#endif



/*Address of bak bootenv parameters in flash*/
HI_U32 g_EnvBakFlashAddr = 0x00200000;

/*At preset, the value is configured to the fixed size:256M. This is not good. We must optimize the issue later*/
HI_U32 g_DDRSize =  0x10000000;					//256M

/*configure for img encrypt and decrypt*/


HI_U8 g_customer_rsa_public_key_N[256]={
            0xd2, 0xce, 0x43, 0x15, 0xcc, 0xa9, 0xba, 0x8a, 0x43, 0x15, 0xe0, 0x37, 0x7c, 0x0d, 0x09, 0x07, 
            0xa8, 0x93, 0xa6, 0xd4, 0x20, 0x6e, 0x1a, 0x1a, 0x61, 0x5a, 0x08, 0xc2, 0x57, 0x35, 0xdb, 0xd2, 
            0xca, 0x9a, 0x41, 0x4d, 0x5c, 0xe6, 0x0d, 0x65, 0xc0, 0x43, 0xa5, 0xed, 0x7e, 0x91, 0x96, 0xf5, 
            0x41, 0x45, 0x0e, 0x63, 0x39, 0xec, 0x73, 0x59, 0xa8, 0xd3, 0x0e, 0x30, 0x95, 0xa4, 0xb4, 0x1c, 
            0xf4, 0x73, 0x89, 0x96, 0x78, 0x77, 0x7b, 0x8f, 0x7f, 0xe3, 0x4a, 0x6f, 0x8f, 0x3e, 0xdb, 0x8d, 
            0xc0, 0x2d, 0x4a, 0x55, 0x98, 0x37, 0xbe, 0xdd, 0x77, 0x34, 0xea, 0x5c, 0xe7, 0xbc, 0x5c, 0xac, 
            0xe9, 0xf4, 0x45, 0x95, 0xf4, 0x25, 0xad, 0x6f, 0xd7, 0xaa, 0x10, 0xe8, 0xd7, 0xbb, 0x90, 0xd9, 
            0x68, 0x5c, 0x64, 0x42, 0x6f, 0x8d, 0xca, 0x7d, 0x22, 0xdf, 0x0e, 0xe9, 0x84, 0xce, 0xbe, 0xc3, 
            0xb1, 0xa0, 0x90, 0xd6, 0x44, 0x72, 0x52, 0xca, 0xc3, 0x0f, 0xef, 0xd5, 0xe4, 0x38, 0x2c, 0xb2, 
            0x55, 0xcf, 0x87, 0x53, 0x9d, 0x32, 0x73, 0x49, 0x38, 0x3c, 0x71, 0x21, 0x66, 0x58, 0xea, 0x7a, 
            0x41, 0x41, 0x8f, 0x4c, 0x3f, 0x8f, 0x0e, 0x88, 0x57, 0xfa, 0xf6, 0x18, 0x42, 0x0e, 0xd1, 0x92, 
            0x93, 0xa6, 0x7c, 0xb3, 0x5e, 0x0d, 0x2b, 0x27, 0x2b, 0x78, 0x69, 0xc4, 0x81, 0x67, 0x1a, 0xf0, 
            0x8c, 0xec, 0x58, 0xd9, 0x07, 0x8e, 0x2c, 0x90, 0xb5, 0x40, 0xb5, 0x6d, 0xf0, 0x2d, 0xbb, 0x8f, 
            0xc8, 0x15, 0x5a, 0xcd, 0xd6, 0x93, 0xac, 0xf3, 0x8a, 0x8d, 0xb4, 0x0f, 0x75, 0x46, 0x7b, 0x30, 
            0xb9, 0x7c, 0x0f, 0x59, 0xce, 0x03, 0x6e, 0xa0, 0x9e, 0xf9, 0x67, 0xb9, 0xf6, 0x72, 0xfe, 0xfe, 
            0x8e, 0x4c, 0x5c, 0x04, 0x89, 0x0b, 0xb5, 0x7c, 0xdf, 0x27, 0x99, 0xbb, 0x23, 0xec, 0xde, 0xe1, 
	        };

HI_U8 g_customer_rsa_public_key_E_100001[256] = {
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x01
            };
HI_U8 g_customer_rsa_public_key_E_10001[256] = {
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01
            };

HI_U8 g_customer_rsa_public_key_E_3[256] = {
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03
            };
            
HI_U32  g_customer_rsa_public_key_E = 0x3;

static HI_U8 G1_CIPHERKEY[16]      = {0x5b,0x45,0x14,0x37,0x6d,0xed,0x1d,0x08,0x64,0x97,0xbc,0xba,0xe6,0x7f,0x90,0x78};
static HI_U8 G2_CIPHERKEY[16]      = {0xd6,0xbc,0x9e,0xe2,0x3a,0x61,0x50,0x10,0xb4,0x64,0x2e,0xaa,0xe8,0xf0,0x53,0xa6};
static HI_U8 G1_KEYLADDER_KEY1[16] = {0x1c,0x21,0x42,0xa2,0x9e,0x31,0x57,0x8d,0xff,0xa4,0xe1,0xdf,0x2e,0x36,0xa0,0x6e};
static HI_U8 G1_KEYLADDER_KEY2[16] = {0x7c,0x76,0x1a,0x38,0xc1,0x46,0x4e,0x53,0x8c,0xa3,0xd7,0xae,0x56,0xee,0x27,0xb7};
static HI_U8 G2_KEYLADDER_KEY1[16] = {0x41,0x98,0x30,0x77,0x14,0xed,0x9d,0xc3,0x60,0x55,0x49,0xcf,0x5e,0x75,0x13,0xa2};
static HI_U8 G2_KEYLADDER_KEY2[16] = {0x5c,0x98,0xbe,0x7c,0x52,0x24,0x35,0xb8,0x30,0xa5,0x10,0x54,0x33,0xc3,0x21,0xf8};


/*algorithm of authenticate,default is sha2,now ,we only support sha2*/
AUTHALG_E g_AuthAlg = AUTH_SHA2;


#if 0
HI_U8  g_customer_blpk[16] = "8288300882883009";
#else
HI_U8  g_customer_blpk[16] = {0x67,0xb1,0xf8,0x28,0xdd,0x93,0x41,0xbc,0xca,0xed,0x73,0xd4,0x8d,0xa8,0xe9,0x1f};
#endif


HI_S32 HI_CA_GetKeyladderKey(HI_CA_KEY_GROUP_E enKeyGroup, HI_U8 u8KeyladderKey1[16], HI_U8 u8KeyladderKey2[16], HI_U8 u8CipherKey[16])
{
    HI_U8 *pu8KeyLadderkey1, *pu8KeyLadderkey2, *pu8CipherKey;
    
    if (enKeyGroup == HI_CA_KEY_GROUP_1)
    {
       pu8KeyLadderkey1 = G1_KEYLADDER_KEY1;
       pu8KeyLadderkey2 = G1_KEYLADDER_KEY2;
       pu8CipherKey = G1_CIPHERKEY;
    }
    else if (enKeyGroup == HI_CA_KEY_GROUP_2)
    {
       pu8KeyLadderkey1 = G2_KEYLADDER_KEY1;
       pu8KeyLadderkey2 = G2_KEYLADDER_KEY2;
       pu8CipherKey = G2_CIPHERKEY;
    }
    else
    {
       return HI_FAILURE;
    }
    
    if (u8KeyladderKey1 != HI_NULL)
    {
        memcpy(u8KeyladderKey1, pu8KeyLadderkey1, KEY_LEN);
    }
    
    if (u8KeyladderKey2 != HI_NULL)
    {
        memcpy(u8KeyladderKey2, pu8KeyLadderkey2, KEY_LEN);
    }
    
    if (u8CipherKey != HI_NULL)
    {
        memcpy(u8CipherKey, pu8CipherKey, KEY_LEN);
    }    

    return HI_SUCCESS;
}

HI_S32 HI_CA_GetBLPKInfor(HI_U8 u8BLPK[16])
{
    if (u8BLPK == HI_NULL)
    {
       return HI_FAILURE;
    }    

    memcpy(u8BLPK, g_customer_blpk, KEY_LEN); 

    return HI_SUCCESS;
}

HI_S32 HI_CA_GetRSAKeyInfor(AUTHALG_E eAuthType, HI_UNF_CIPHER_RSA_PUB_KEY_S *pstPubKey)
{
    if (pstPubKey == HI_NULL)
    {
       return HI_FAILURE;
    }

     pstPubKey->pu8N = g_customer_rsa_public_key_N;
     pstPubKey->u16NLen = sizeof(g_customer_rsa_public_key_N);
     if (g_customer_rsa_public_key_E == 0x3)
     {
         pstPubKey->pu8E = g_customer_rsa_public_key_E_3;
     }
     else if (g_customer_rsa_public_key_E == 0x100001)
     {
         pstPubKey->pu8E = g_customer_rsa_public_key_E_100001;
     }
     else if (g_customer_rsa_public_key_E == 0x10001)
     {
         pstPubKey->pu8E = g_customer_rsa_public_key_E_10001;
     }
     else
     {
        return HI_FAILURE;
     }
     
     pstPubKey->u16ELen = 256;    

    return HI_SUCCESS;
}